﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:张谦
// ===============================================================================

using System;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmUserDetails : BaseForm
    {
        public FrmUserDetails(string id)
        {
            InitializeComponent();
            tabPane.RecordId = id;
        }

        public FrmUserDetails(GridControlPlus gridControl)
        {
            InitializeComponent();
            tabPane.Grid = gridControl;
        }

        private async void FrmUserDetails_Load(object sender, EventArgs e)
        {
            await tabPane.BindingModelAsync();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogCancel();
        }

        private void EditDept_Click(object sender, EventArgs e)
        {
            var model = bindingSource.DataSource as SysUser;
            if (model == null) return;
            new FrmDeptDetails(model.DeptId).ShowDialogOk();
        }

        private void TabPane_PageBinding(object sender, DetailsFormPaneBindingEventArgs e)
        {
            e.Task = async () =>
            {
                switch (e.Current.TabPageName)
                {
                    case nameof(tabBasic):
                        var model = string.IsNullOrEmpty(e.Current.Id) ? new SysUser() : await SysUserService.Instance.SelectByIdAsync(e.Current.Id);
                        layItemEnt.Visibility = WinFormHelper.LayoutItemVisible(model.Category == 1);
                        bindingSource.DataSource = model;
                        break;
                    case nameof(tabHaveRole):
                        await roleListUserControl1.LoadAsync(e.Current.Id);
                        break;
                    case nameof(tabModulePermission):
                        await moduleOwnerTreeList1.LoadByUserIdAsync(e.Current.Id);
                        break;
                    case nameof(tabDeptPermission):
                        await deptOwnerTreeList1.LoadByUserAsync(e.Current.Id);
                        break;
                    case nameof(tabLoginLog):
                        await userOwnerLoginLogUserControl1.LoadAsync(e.Current.Id);
                        break;
                    case nameof(tabOperateLog):
                        await userOwnerOperateLogUserControl1.LoadAsync(e.Current.Id);
                        break;
                    case nameof(tabHistory):
                        await historyLogGrid1.LoadAsync(nameof(SysUser), e.Current.Id);
                        break;
                }
            };
        }
    }
}